/**
* @File:ring_buffer.h
* @Version:V1.0
* @Author:rudius
* @Brief:
* @Logs:
* 	Date		Author		Notes
* 	2025-06-17	rudius		first version
*/

#ifndef __RING_BUFFER_H
#define __RING_BUFFER_H
// <<< Use Configuration Wizard in Context Menu >>>

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stddef.h>
#include <rtthread.h>
#ifdef __cplusplus
extern "C" {
#endif

/*----------------------------------------------------------------------------*/

/* Public define -------------------------------------------------------------*/
/* 环形缓冲区对齐设置 */
#define RINGBUF_ALIGNMENT 4
/* Public macro --------------------------------------------------------------*/
/* Public typedef ------------------------------------------------------------*/
/* 环形缓冲区结构体 */
struct ring_buf
{
    rt_uint8_t     *buffer_ptr;      /* 缓冲区指针 (对齐到4字节边界) */
    rt_size_t       buffer_size;     /* 缓冲区实际大小（字节） */
    
    rt_size_t       read_index;      /* 读索引 */
    rt_size_t       write_index;     /* 写索引 */
    rt_bool_t       is_full;         /* 缓冲区满标志 */
    
#ifdef USER_RING_BUFFER_THREAD_SAFE
    struct rt_mutex lock;            /* 互斥锁 (线程安全时使用) */
#endif
};

typedef struct ring_buf ring_buf_t;
/* Public variables ----------------------------------------------------------*/
/* Public function prototypes ------------------------------------------------*/
/* 公开API接口 */
rt_err_t ring_buf_init(ring_buf_t *rb, rt_size_t size);
void ring_buf_detach(ring_buf_t *rb);
ring_buf_t *ring_buf_create(rt_size_t size);
void ring_buf_destroy(ring_buf_t *rb);

rt_size_t ring_buf_put(ring_buf_t *rb, const rt_uint8_t *data, rt_size_t length);
rt_size_t ring_buf_get(ring_buf_t *rb, rt_uint8_t *data, rt_size_t length);
rt_size_t ring_buf_data_size(ring_buf_t *rb);
rt_bool_t ring_buf_is_empty(ring_buf_t *rb);
rt_bool_t ring_buf_is_full(ring_buf_t *rb);
void ring_buf_reset(ring_buf_t *rb);

#ifdef __cplusplus
}
#endif

// <<< end of configuration section >>>
#endif
/*------------------------------end of ring_buffer.h---------------------------------*/